﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Mozlite.Components;
using System.Web.Routing;

namespace Mozlite
{
    /// <summary>
    /// 全局辅助方法类。
    /// </summary>
    public static class Globals
    {
        /// <summary>
        /// 禁用词语过滤实例。
        /// </summary>
        public static IWordFilter WordFilter {
            get {
                var cacheKey = "WordFilters";
                return Caches.Get<IWordFilter>(cacheKey, () =>
                {
                    var wordFilter = new TrieFilter();
                    var path = Cores.MapPath("~/App_Data/BadWord.moz");
                    using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read))
                    {
                        var reader = new StreamReader(fs, Encoding.UTF8);
                        var words = reader.ReadLine().NullSafeTrim();
                        while (!reader.EndOfStream)
                        {
                            if (words.HasValue())
                                wordFilter.AddKey(words);
                            words = reader.ReadLine().NullSafeTrim();
                        }
                        reader.Close();
                        fs.Close();
                    }
                    Caches.Insert(cacheKey, wordFilter, new System.Web.Caching.CacheDependency(path));
                    return wordFilter;
                });
            }
        }

        /// <summary>
        /// 叠代循环所有子对象。
        /// </summary>
        /// <typeparam name="T">对象类型。</typeparam>
        /// <param name="obj">对象实例。</param>
        /// <param name="action">执行方法。</param>
        public static void Recurse<T>(this T obj, Action<T> action) where T : IRecursion<T>
        {
            if (action != null)
                action(obj);
            if (obj.HasChildren)
            {
                foreach (var t in obj.Children)
                    t.Recurse<T>(action);
            }
        }

        /// <summary>
        /// 循环执行方法。
        /// </summary>
        /// <typeparam name="T">类型。</typeparam>
        /// <param name="obj">列表对象。</param>
        /// <param name="action">执行方法。</param>
        public static void ForEach<T>(this IEnumerable<T> obj, Action<T, int> action)
        {
            var i = 0;
            foreach (var item in obj)
            {
                action(item, i++);
            }
        }

        #region resources
        /// <summary>
        /// 获取当前语言资源字符串。
        /// </summary>
        /// <param name="key">资源键对象。</param>
        /// <returns>返回资源字符串。</returns>
        public static string ToLocal(object key)
        {
            return Cores.GetGlobalResourceByKey(key);
        }

        /// <summary>
        /// 获取当前语言资源字符串。
        /// </summary>
        /// <param name="key">资源键对象。</param>
        /// <param name="args">格式化参数。</param>
        /// <returns>返回格式化后的资源字符串。</returns>
        public static string ToLocal(object key, params object[] args)
        {
            return Cores.GetGlobalResourceByKey(key).FormatWith(args);
        }
        #endregion

        /// <summary>
        /// 获取区域语言列表。
        /// </summary>
        public static List<string> Lanaguages {
            get
            {
                const string cacheKey = "Lanaguages";
                return Caches.Get<List<string>>(cacheKey, () =>
                {
                    var dir = new DirectoryInfo(Cores.GetPath("~/App_GlobalResources"));
                    var languages = new List<string>();
                    languages.Add("zh-CN");
                    foreach (var file in dir.GetFiles("Resource.*.resx"))
                    {
                        var language = file.Name.Substring(9, file.Name.Length - 14);
                        languages.Add(language);
                    }
                    Caches.Max(cacheKey, languages, new System.Web.Caching.CacheDependency(dir.FullName));
                    return languages;
                });
            }
        }
        /// <summary>
        /// 聚合两个集合。
        /// </summary>
        /// <param name="dest">目标集合。</param>
        /// <param name="src">附加的集合。</param>
        /// <param name="replaceSource">如果目标已经存在是否替换。</param>
        /// <returns>返回附加后的集合。</returns>
        public static RouteValueDictionary Merger(this RouteValueDictionary dest, IDictionary<string, object> src, bool replaceSource = true)
        {
            foreach(var kv in src) {
                if(dest.ContainsKey(kv.Key))
                {
                    if(replaceSource)
                        dest[kv.Key] = kv.Value;
                }
                else
                    dest.Add(kv.Key, kv.Value);
            }
            return dest;
        }
    }
}
